/*** 
 * Class Name: IfulfillSyncWithQueue
 * Author: Accenture
 * Date: 13-Jun-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: Inserts/deletes users into Standard Queues whenever members are added/deleted from assignment groups for fulfillment item ONLY
 **/

public class IfulfillSyncWithQueue{
    @future //Limitation of sfdc that cannot update std and custom objects in same transaction. So annotated as future. Also only primitive datatype can be passed as parameter    
    public static void createQueueMember(set<Id> AGMemIds) // add member to Queue if members are added to assignment group
    {  
            Map<Id,Id> AGQueueMap = new Map<Id,Id>();
            set<id> AGIds = new set<id>();
            list<GroupMember > grpMembList = new list<GroupMember >(); 
            //get all the newly added members
            list<Assignment_Groups__c>  AG = [SELECT id,name,Group_Name__c,User__C
                                                   FROM Assignment_Groups__c
                                                       WHERE Id in :AGMemIds and (Group_Name__r.Type__c = :IfullfillTextConstant.ASSIGNMENT_GROUP_TYPE  or Group_Name__r.Type__c = :IfullfillTextConstant.ASSIGNMENT_GROUP_QUERY_TYPE)];       
            for(Assignment_Groups__c memb :AG)
            {
               AGIds.add(memb.Group_Name__c);  
            }
            
            //collect the queues which have to be updated/synced
            for(Assignment_Group_Queues__c AGQueue:[SELECT Id, Name, QueueId__c, Assignment_Group_Name__c
                                                        FROM Assignment_Group_Queues__c
                                                            WHERE Assignment_Group_Name__c in :AGIds]){
               AGQueueMap.put(AGQueue.Assignment_Group_Name__c, Id.valueof(AGQueue.QueueId__c) );   
            }
            
            //add members to the queue
            for(Assignment_Groups__c AGmemb :AG)
            {
                if(AGQueueMap.containsKey(AGmemb.Group_Name__c))
                {
                    GroupMember grMemb = new GroupMember();
                    grMemb.GroupId  = AGQueueMap.get(AGmemb.Group_Name__c);
                    grMemb.UserOrGroupId   = AGmemb.User__C;
                    grpMembList.add(grMemb);
                 } 
            }    
            try
            {
                Insert grpMembList;
            }
            Catch(dmlException e)
            {
                GeneralConstants.mailAnyExceptionToMe('IfulfillSyncWithQueue','createQueueMember', e);
            }
    }
    
    @future
    public static void deleteQueueMember( Set<ID> userID ,set<Id> AGIds) // delete member from Queue if members are deleted from assignment group
    {   
        Set<String> queueName = new Set<String>();
        //collect the Queues which need to be updated/synced
        for(Assignment_Group_Queues__c AGQueue:[SELECT  Name,  Assignment_Group_Name__c FROM Assignment_Group_Queues__c 
                                                   WHERE Assignment_Group_Name__c in :AGIds])
        {
            queueName.add(AGQueue.name);
        }
        
        // collect members that have to be deleted
        List<GroupMember>  member1 = [select id, UserOrGroupId  from GroupMember where UserOrGroupId   in  :userID 
                                        and GroupId in (select id from group where name in :queueName)];
        if(member1.size()> 0)
        {
            try{delete member1;}
            Catch(dmlException e)
            {
                GeneralConstants.mailAnyExceptionToMe('IfulfillSyncWithQueue','createQueueMember', e);
            }
            
        }  
     }  
  }